package tree.traversal;

import tree.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author justnow
 * Created on 2020-08-24
 * Description
 */
public class LevelSort2 {

    public void levelSort(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<Integer> array = new ArrayList<>();
        queue.add(root);
        while (queue.size() > 0) {
            TreeNode current = queue.poll();
            array.add(current.val);
            if (current.left != null) queue.add(current.left);
            if (current.right != null) queue.add(current.right);
        }
        for (Integer integer : array) {
            System.out.println(integer);
        }
    }
}
